#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
//#extension GL_ARB_gpu_shader_int64 : require



#include "../../../../core/shader_std.h"
#include "../../../../core/shader_built_in_functions.h"
#include "../../../../core/shader_mesh.h"


layout(location = 0) in vec3 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inUV;


out gl_PerVertex {
	vec4 gl_Position;
};

layout(location = 0) out vec3 outEyeDir;
layout(location = 1) out vec3 outNormal;
layout(location = 2) out vec4 outVert;
layout(location = 3) out mat4 outProj;
layout(location = 7) out uint outMaterialID;


layout(std140, set = 0, binding = 0) uniform Buf0 {
	mat4 mat_ProjecView[4];
};

layout(std430, set = 1, scalar, column_major, binding = 1) readonly buffer Buf1 {
	mat4 mat_Model[];
};

layout(std430, set = 1, scalar, column_major, binding = DEF_BINDING_LineProp) readonly buffer Buf3 {
	S_MeshTranformProp m_LinesProp[];
};

layout(std430, set = 1, scalar, column_major, binding = DEF_BINDING_WIDGET_Color) readonly buffer Buf2 {
	uint InstanceColor[];
};

layout(push_constant, scalar) uniform DRAWPROP_push_constants {
	S_Vert_MeshDrawProp const_push;
};



void main(void){
	mat4 bonesMat_som = mat_Model[gl_InstanceIndex + const_push.MatrixOffset];

	S_MeshTranformProp prop = m_LinesProp[const_push.MeshPropOffset + gl_DrawID];

	vec3 camPos = (inverse(mat_ProjecView[3]) * vec4(0,0,0,1)).xyz;
	vec4 p = vec4(inPos, 1);
	
	if ( f_lineMeshTranform(p, inNormal, prop, bonesMat_som, (mat3(mat_ProjecView[1])), camPos) ) {
		//outVert = p;
		outVert = bonesMat_som * p;
	}
	else {
		outVert = p;
	}
	//outVert = bonesMat_som * p;
	//gl_Position = mat_ProjecView[0] * p;
	outProj = mat_ProjecView[DEF_Camera_Matrix_PV];

	//outVert = bonesMat_som * p;
	outEyeDir = outVert.xyz - camPos;
	outNormal = normalize(mat3(bonesMat_som) * inNormal);
	outMaterialID = prop.m_MaterialID;
	
	//outProj = mat_ProjecView[DEF_Camera_Matrix_View];

	gl_Position = mat_ProjecView[0] * outVert;
}


